#include "spacedots.h"
#include "rockdodger_globals.h"
#include "random_gen.h"
#if DEBUG > 0
#include <stdio.h>
#endif

static struct spacedot sdot[MAX_SPACE_DOTS];

void init_space_dots(SDL_Surface * surf_screen) {
  int i, intensity;
  for(i = 0; i < MAX_SPACE_DOTS; i++) {
    float r = rnd_next_float();	// We are in init and can take our time.

    sdot[i].x = rnd() * (xsize - 5);
    sdot[i].y = rnd() * (ysize - 5) + 2.5;

    r = r * r;
    sdot[i].dx = -r * 4;
    // -1/((1-r)+.3);
    intensity = (int) (r * 180 + 50);
    sdot[i].color =
      SDL_MapRGB(surf_screen->format, intensity, intensity, intensity);
#if DEBUG > 0
    printf("r=%E sdot.dx=%E\n", r, sdot[i].dx);
#endif
  }
}

void draw_space_dots(SDL_Surface * s) {
  int i;
  RD_VIDEO_TYPE *rawpixel = (RD_VIDEO_TYPE *) s->pixels;

  for(i = 0; i < MAX_SPACE_DOTS; ++i) {
    rawpixel[s->pitch / 2 * sdot[i].y + (int) (sdot[i].x)] = sdot[i].color;
    sdot[i].x += sdot[i].dx * movementrate;
    if(sdot[i].x < 0)
      sdot[i].x = xsize;
  }
}
